package com.Servlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Servlet.Dao.usersEntity;

@WebServlet("/bookStyleInsertServlet")
public class bookStyleInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String URL = "images/1.jpg";
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("utf-8");
		response.setCharacterEncoding("utf-8");
		String[] values = request.getParameterValues("bookInfo");
		String userSno = request.getParameter("userSno");
		request.setAttribute("userSno", userSno);
		request.setAttribute("flag", "bookStyleInsert");
		try {
			String check = "true";
			ArrayList<Object> al = new ArrayList<>();
			for(int i = 0; i < values.length; i++) {
				switch(i) {
					case 5:
						File file = new File(values[i]);
						if(!file.exists() || values[i] == null) {
							values[i] = URL;
						}
						break;
					default :
						if(values[i] == null) {
							check = "false";
						}
				}
				if(check.equals("false")) {
					break;
				}
				al.add(values[i]);
			}
			if(check.equals("true")) {
				usersEntity.bookStyleInsert(al);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("Main.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
